package com.zbec.mdrs.repository.jpa;

import com.zbec.mdrs.entity.discount.Discount;
import com.zbec.mdrs.entity.entrustedLoan.EntrustedLoan;
import com.zbec.mdrs.entity.guarantee.Guarantee;
import com.zbec.mdrs.vo.GuaranteeVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by fxl on 2015/10/17.
 */
public interface GuaranteeDao extends PagingAndSortingRepository<Guarantee, Long>, JpaSpecificationExecutor<Guarantee> {

    List<Guarantee> findByHistoryId(Long historyId);

    List<Guarantee> findByHistoryIdAndOrgId(Long historyId,Long orgId);

    @Query("select new com.zbec.mdrs.vo.GuaranteeVo(g.id,g.dbjgzzdm, g.jrjgbm,g.dbhth,g.bdbrzzjgdm,g.sjsj,g.bz,g.dbywzl," +
            "g.dbye,g.dbksrq,g.dbdqrq,g.wjfl,g.dydcfsje,g.dydbfsje,g.dyfsbs,g.zbbs,g.source,g.recordStatus,g.reportStatus," +
            "g.existError,g.errorDesc,g.missedField,g.recordId) from Guarantee g  " +
            "where (''= :sjsj or g.sjsj = :sjsj) "+
            "and (''= :recordStatus or g.recordStatus = :recordStatus) " +
            "and (''= :existError or g.existError = :existError)  ")
    Page<GuaranteeVo> findAllGuarantee(@Param("sjsj") String sjsj, @Param("recordStatus") String recordStatus, @Param("existError") String existError, Pageable pageable);

    @Query("select new com.zbec.mdrs.vo.GuaranteeVo(g.id,g.dbjgzzdm, g.jrjgbm,g.dbhth,g.bdbrzzjgdm,g.sjsj,g.bz,g.dbywzl," +
            "g.dbye,g.dbksrq,g.dbdqrq,g.wjfl,g.dydcfsje,g.dydbfsje,g.dyfsbs,g.zbbs,g.source,g.recordStatus,g.reportStatus," +
            "g.existError,g.errorDesc,g.missedField,g.recordId) from Guarantee g  " +
            "where (''= :historyId or g.history.id = :historyId) ")
    Page<GuaranteeVo> findGuaranteeByHistroy(@Param("historyId")String historyId,Pageable pageable);


    @Query("select l from Guarantee l  " +
            "where (''= :sjsj or l.sjsj = :sjsj) "+
            "and (''= :recordStatus or l.recordStatus = :recordStatus) " +
            "and (''= :existError or l.existError = :existError) order by l.dbjgzzdm ")
    List<Guarantee>  findGuaranteeForExport(@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus,@Param("existError")String existError);




    @Query( "select max(sjsj) from Guarantee where sjsj < :sjsj")
    String getMaxGuaranteeFny(@Param("sjsj")String sjsj);

    @Query("select l from Guarantee l where l.recordStatus !=3 and l.recordId = :recordId and l.sjsj = :sjsj ")
    Guarantee getGuaranteeFromDrmap(@Param("recordId")Long recordId,@Param("sjsj")String sjsj);

    @Query("select l from Guarantee l  " +
            "where l.sjsj = :sjsj  and l.recordStatus = :recordStatus  and  l.reportStatus <> :reportStatus " +
            "and (''= :jrjgbm or l.jrjgbm = :jrjgbm) order by l.dbjgzzdm  ")
    List<Guarantee> getActiveGuaranteeByYwMonth(@Param("sjsj")String sjsj ,@Param("recordStatus")String recordStatus,
                                                @Param("reportStatus")String reportStatus,@Param("jrjgbm")String jrjgbm);
}
